package com.klikmee.sms.chat.web.usecases;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import com.dotexe.webframework.core.ClientApplication;
import com.dotexe.webframework.core.clientactions.MessageBox;
import com.dotexe.webframework.core.datasources.CollectionDataSource;
import com.dotexe.webframework.core.triggers.Trigger;
import com.klikmee.sms.chat.web.dao.TemplateDAO;
import com.klikmee.sms.chat.web.util.SpringBeansLookup;
import com.klikmee.sms.services.chat.model.Template;

public class TemplateListForm extends ClientApplication {

	private CollectionDataSource dsTemplates;
	private ArrayList<String> bcTemplates;
	
	private List<Template> templates;
	
	private TemplateDAO templateDAO;
	
	private TemplateDetailsForm details;
	
	public void load() throws Exception {
		super.load();

		this.dsTemplates = new CollectionDataSource("dsTemplates");
		this.getDataSources().add(this.dsTemplates);
		this.bcTemplates = new ArrayList<String>();
		this.bcTemplates.add("Name");
		this.bcTemplates.add("Template");
		this.bcTemplates.add("Active");
		
		this.triggers.add(new Trigger(this, "btnEditTemplateClick"));
		this.triggers.add(new Trigger(this, "btnAddTemplateClick"));
		this.triggers.add(new Trigger(this, "btnDeleteTemplateClick"));
		this.triggers.add(new Trigger(this, "gvTemplatesCellClick"));
		this.triggers.add(new Trigger(this, "gvTemplatesCellDoubleClick"));
	}

	@Override
	public void start() throws Exception {
		super.start();
		
		bindTemplateList();
	}

	public void btnEditTemplateClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		if (this.dsTemplates.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati template!");
			return;
		} else {
			details = createChildApplication(TemplateDetailsForm.class);
			details.setTemplate((Template) this.dsTemplates.getSelectedItem());
			details.triggers.get("btnSaveClick").addOnFire(this, "afterTemplateSave");
			details.setEditTemplate(true);
			this.showChildApp(details);
			details.start();
		}
	}

	public void btnAddTemplateClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		details = createChildApplication(TemplateDetailsForm.class);
		Template newTemplate = new Template();
		newTemplate.setActive(true);
		details.setTemplate(newTemplate);
		details.triggers.get("btnSaveClick").addOnFire(this, "afterTemplateSave");
		details.setEditTemplate(false);
		this.showChildApp(details);
		details.start();
	}

	public void btnDeleteTemplateClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		if (this.dsTemplates.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati template!");
			return;
		} else {
			Template temp = (Template) this.dsTemplates.getSelectedItem();
			temp.setActive(false);
			templateDAO.edit(temp);
			bindTemplateList();
		}
	}
	
	public void gvTemplatesCellClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
	}
	
	public void gvTemplatesCellDoubleClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		details = createChildApplication(TemplateDetailsForm.class);
		details.setTemplate((Template) this.dsTemplates.getSelectedItem());
		details.triggers.get("btnSaveClick").addOnFire(this, "afterTemplateSave");
		details.setEditTemplate(true);
		this.showChildApp(details);
		details.start();
	}

	public void afterTemplateSave() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		bindTemplateList();
	}

	private void bindTemplateList() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		templateDAO = SpringBeansLookup.getBean(TemplateDAO.class);
		templates = templateDAO.findAll();
		this.dsTemplates.setSource(templates);
		this.dsTemplates.bind(bcTemplates);
	}
}
